<template>
  <div class="idadd">
    <el-dialog
      :title="info ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :inline="true"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="author">
          <el-input
            v-model="PmsBrand.username"
            placeholder="用户名"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="author">
          <el-input
            v-model="PmsBrand.nickName"
            placeholder="用户昵称"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="author">
          <el-input
            v-model="PmsBrand.password"
            placeholder="请输入密码"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户头像" prop="author">
          <div style="width: 300px">
            <el-upload
              ref="upload"
              :action="uploadUrl"
              :headers="headers"
              :on-success="handleAvatarSuccess"
            >
              <i
                v-if="PmsBrand.salt == ''"
                class="el-icon-user-solid aboutter"
              ></i>
            </el-upload>
            <img
              v-if="PmsBrand.salt"
              :src="PmsBrand.salt"
              style="height: 50px"
              alt=""
            />
          </div>
        </el-form-item>
        <el-form-item label="选择角色" prop="author">
          <el-select
            v-model="PmsBrand.roleIds"
            multiple
            placeholder="请选择"
            size="small"
          >
            <el-option
              v-for="item in tableDataes"
              :key="item.value"
              :label="item.label"
              :value="item.roleName"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="footers">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import authority from '@/api/authority'
import mixins from '@/mixins'
export default {
  mixins: [mixins],
  data() {
    return {
      tableDataes: '',
      info: '',
      dialogVisible: false,
      value: '',
      value1: '',
      PmsBrand: {
        id: '',
        nickName: '', //用户昵称
        password: '', //密码
        roleIds: ',',
        salt: '',
        username: '', //用户名
      },
    }
  },
  created() {},
  mounted() {
    this.findRolesByPage()
  },
  methods: {
    submit() {
      this.dialogVisible = false

      if (this.info) {
        authority
          .updateUserRoles({
            id: this.info.id,
            nickName: this.PmsBrand.nickName,
            password: this.PmsBrand.password,
            username: this.PmsBrand.username,
            salt: this.PmsBrand.salt,
            roleIds: this.PmsBrand.roleIds,
          })
          .then(res => {
            console.log(res, '编辑')
            this.$parent.findUsersByPage()
          })
      } else {
        authority
          .saveUserRolesApi({
            // id: '',
            nickName: this.PmsBrand.nickName,
            password: this.PmsBrand.password,
            username: this.PmsBrand.username,
            salt: this.PmsBrand.salt,
            // roleIds: this.PmsBrand.roleIds,
          })
          .then(res => {
            console.log(res, 'res')
            this.$parent.findUsersByPage()
          })
      }
      this.PmsBrand = ''
    },
    cancle() {
      this.dialogVisible = false
      this.PmsBrand.id = ''
      this.PmsBrand.nickName = ''
      this.PmsBrand.password = ''
      this.PmsBrand.roleIds = ''
      this.PmsBrand.salt = ''
      this.PmsBrand.username = ''
      this.$parent.findUsersByPage()
    },
    //子组件的打开方法
    open(data) {
      this.dialogVisible = true

      if (data) {
        console.log(data, 'data')

        this.PmsBrand = data
        this.PmsBrand.nickName = data.nick_name
        this.info = data
        console.log(this.info, 'this.info')
      } else {
        this.info = ''
      }
    },
    handleAvatarSuccess(res) {
      console.log(res, 'resss')
      if (res.success) {
        this.PmsBrand.salt = res.data.fileUrl
        // this.$refs.upload.clearFiles()
      } else {
        this.$message.error('上传失败')
      }
    },
    //获取文章列表
    findRolesByPage() {
      authority
        .findRolesByPageApi(
          this.page.currentPage,
          this.page.pageSize,
          this.from
        )
        .then(res => {
          console.log(res, 'res')
          if (res.success) {
            this.tableDataes = res.data.rows //获取到线上文字地址
            this.page.total = res.data.total //获取文章总条数
          } else {
            this.$message.error('请求失败')
          }
          console.log(this.tableDataes, '666')
        })
    },
  },
}
</script>

<style scoped lang="scss">
.idadd ::v-deep .el-dialog {
  height: 500px !important;
}
.cadade {
  width: 500px;
  margin: 10px 0;
  float: left;
}
.el-input {
  width: 200px;
}

.footers {
  position: absolute !important;
  bottom: 10px !important;
  right: 10px;
}
.switchsed {
  position: absolute;
  top: 250px;
  left: 120px;
}
.switchses {
  position: absolute;
  top: 250px;
  left: 620px;
}
.aboutter {
  color: #fff;

  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background-color: #c0c4cc;
  border-radius: 30px;
}
</style>